package com.yycx.module.bus.provider.handler.haiying.tasktodo.settle.pay;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.BusHyorderInovice;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.bus.provider.service.hy.BusHyorderInoviceDetailsService;
import com.yycx.module.bus.provider.service.hy.BusHyorderInoviceService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.engine.RuntimeService;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 结算费用申请审批任务列表
 *
 * @author zyf
 */
@Component
public class HySettlePayTab1TaskTodoHandler implements FlowPageInterceptor<TaskResponse> {

    @Autowired
    private BusPaymentService paymentService;


    @Autowired
    private BusDeptService deptService;




    @Autowired
    protected RuntimeService runtimeService;

    @Autowired
    private BusHyorderInoviceDetailsService orderInoviceDetailsService;

    @Autowired
    private BusHyorderInoviceService hyorderInoviceService;


    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        Long companyId = OpenHelper.getCompanyId();
        String paymentNo = params.get("paymentNo");
        String deptId = params.get("deptId");
        String supplierId = params.get("supplierId");
        String customId = params.get("customId");
        String amountType = params.get("amountType");
        String taskName = params.get("taskName");
        String linkMan = params.get("linkMan");
        BusPayment payment = paymentService.getByProperty("paymentNo", paymentNo);
        if (FlymeUtils.isNotEmpty(payment)) {
            taskQuery.processInstanceBusinessKey(payment.getPaymentId().toString());
        } else {
            if (FlymeUtils.isNotEmpty(paymentNo)) {
                taskQuery.processVariableValueLike("paymentNo", '%' + paymentNo);
            }
        }
        if (FlymeUtils.isNotEmpty(amountType)) {
            taskQuery.processVariableValueEquals("amountType", amountType);
        }
        if (FlymeUtils.isNotEmpty(deptId)) {
            taskQuery.processVariableValueEquals("deptId", Long.parseLong(deptId));
        }
        if (FlymeUtils.isNotEmpty(linkMan)) {
            taskQuery.processVariableValueLike("linkMan", linkMan);
        }
        if (FlymeUtils.isNotEmpty(supplierId)) {
            taskQuery.processVariableValueEquals("supplierId", Long.parseLong(supplierId));
        }
        if (FlymeUtils.isNotEmpty(customId)) {
            taskQuery.processVariableValueEquals("customId", Long.parseLong(customId));
        }
        if (FlymeUtils.isNotEmpty(taskName)) {
            taskQuery.taskName(taskName);
        }
        //排除发起人节点
        taskQuery.taskDefinitionKeyLike("Activity_%");
        //只查看当前账套数据
        taskQuery.processVariableValueEquals("companyId", companyId);
        //关联租户ID
        taskQuery.taskTenantId("flp").orderByTaskCreateTime().desc();
        taskQuery.processDefinitionKey("process_settle_invoice_flp");
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        String companyName = OpenHelper.getAttr("companyName", String.class);
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = new EntityMap();
                map.put("processInstanceId", taskResponse.getProcessInstanceId());
                String paymentId = taskResponse.getBusinessKey();
                String taskName = taskResponse.getName();
                String taskDefinitionKey = taskResponse.getTaskDefinitionKey();
                if (FlymeUtils.isNotEmpty(paymentId)) {
                    //办理标记
                    map.put("handle", 1);
                    //是否可以申请开票标记
                    map.put("invoiceApply", 0);
                    //填写发票号码
                    map.put("invoicing", 0);
                    //支付开票税金
                    map.put("invoicePay", 0);
                    //市场结算支付
                    map.put("settlePay", 0);
                    //是否可以删除结算明细
                    map.put("deleteDetails", 0);
                    EntityMap payment = paymentService.findById(paymentId);
                    if (FlymeUtils.isNotEmpty(payment)) {
                        Long invoiceId = payment.getLong("invoiceId");
                        if (FlymeUtils.isNotEmpty(invoiceId)) {
                            BusHyorderInovice hyorderInovice = hyorderInoviceService.getById(invoiceId);
                            map.put("invoiceNo", hyorderInovice.getInvoiceNo());
                            map.put("invoiceUnitName", hyorderInovice.getInvoiceUnitName());
                            map.put("invoiceBankName", hyorderInovice.getInvoiceBankName());
                            map.put("invoiceBankNo", hyorderInovice.getInvoiceBankNo());
                            map.put("invoiceTitle", hyorderInovice.getTitle());
                            map.put("baopiao", hyorderInovice.getBaopiao());
                            map.put("invoiceType", hyorderInovice.getInvoiceType());
                        }
                        Long deptId = payment.getLong("deptId");
                        map.putAll(payment);
                        map.put("companyName", companyName);
                        if (FlymeUtils.isNotEmpty(deptId)) {
                            BusDept dept = deptService.getById(deptId);
                            map.put("deptName", dept.getDeptName());
                        }
                        Long saleManId = payment.getLong("saleManId");
                        map.put("saleManId", saleManId);
                        map.put("assignee", taskResponse.getAssignee());
                        map.put("taskName", taskName);
                        map.put("taskDefinitionKey", taskDefinitionKey);
                        map.put("taskId", taskResponse.getId());
                        map.put("endTime", taskResponse.getEndTime());


                        //合同专员节点
                        if (taskDefinitionKey.equals("Activity_ROLE_SW_HTZY")) {
                            map.put("invoiceApply", 1);
                            //结算金额
                            BigDecimal invoiceAmount = payment.getBigDecimal("invoiceAmount");
                            //已全部开票
                            if (FlymeUtils.gtzero(invoiceAmount)) {
                                map.put("handle", 1);
                            } else {
                                map.put("handle", 0);
                            }
                        }
                        //已废弃(后续如果添加需要修改流程)
                        if (taskName.equals("开票员开发票")) {
                            //未填写开票号
                            Integer invoiceCount = paymentService.countInvoiceNum(paymentId);
                            map.put("invoicing", 1);
                            if (invoiceCount > 0) {
                                map.put("handle", 0);
                            }
                        }
                        //销售部长||商务复核员 || 商务组长
                        if (taskDefinitionKey.equals("Activity_ROLE_XS_BZ") || taskDefinitionKey.equals("Activity_ROLE_SW_FHY") || taskDefinitionKey.equals("Activity_ROLE_SW_SCZZ")) {
                            map.put("deleteDetails", 1);
                        }
                        //结算给个体户
                        if (taskDefinitionKey.equals("Activity_invoicePay")) {
                            map.put("invoicePay", 1);
                        }
                        //已废弃(后续如果添加需要修改流程)
                        if (taskName.equals("结算给市场")) {
                            //未支付数量
                            Integer payCount = orderInoviceDetailsService.countPayNum(paymentId);
                            map.put("settlePay", 1);
                            if (payCount > 0) {
                                map.put("handle", 0);
                            }
                        }
                    }
                    list.add(map);
                }
            }
        }
        return list;
    }
}
